package com.finer.service.impl;

import com.finer.controller.FinerCustomerController;
import com.finer.dao.FinerPhoneDao;
import com.finer.entity.ResponseMessage;
import com.finer.entity.document.FinerCustomer;
import com.finer.dao.FinerCustomerDao;
import com.finer.service.FinerCustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 客户表(FinerCustomer)表服务实现类
 *
 * @author makejava
 * @since 2024-10-21 10:20:37
 */
@Service("finerCustomerService")
@Transactional
public class FinerCustomerServiceImpl implements FinerCustomerService {
    @Resource
    private FinerCustomerDao finerCustomerDao;

    @Resource
    private FinerPhoneDao  finerPhoneDao;

    private Logger logger = LoggerFactory.getLogger(FinerCustomerServiceImpl.class);

    @Override
    public ResponseMessage getCustomerList(Map map) {
        logger.info("FinerCustomerServiceImpl getCustomerList start....map:"+map);
        ResponseMessage responseMessage = new ResponseMessage();
        int pageNo = Integer.parseInt(map.get("pageNo").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        PageHelper.startPage(pageNo, pageSize);
        List<FinerCustomer> finerCustomerList = finerCustomerDao.getCustomerList(map);
        logger.debug("FinerCustomerServiceImpl getCustomerList end  resuly:"+finerCustomerList);
        PageInfo<FinerCustomer> pageInfo = new PageInfo<>(finerCustomerList);
        responseMessage.setCode("200");
        responseMessage.setData(pageInfo);
        return responseMessage;
    }

    @Override
    public ResponseMessage delCus(Map map) {
        logger.info("FinerCustomerServiceImpl delCus start....map:"+map);
        ResponseMessage responseMessage = new ResponseMessage();
        int num = finerCustomerDao.delCus(map);
        logger.debug("FinerCustomerServiceImpl delCus end  resuly:"+num);
        if (num < 0) {
            responseMessage.setCode("500");
            responseMessage.setMessage("删除失败");

            return responseMessage;
        }
        responseMessage.setCode("200");
        responseMessage.setMessage("删除成功");
        return responseMessage;
    }

    @Override
    public ResponseMessage addCus(Map map) {
        logger.info("FinerCustomerServiceImpl addCus start....map:"+map);
        ResponseMessage responseMessage = new ResponseMessage();
        String cusCode ="kh"+ System.currentTimeMillis();
        map.put("cusCode",cusCode);
        int num = finerCustomerDao.addCus(map);
        logger.debug("FinerCustomerServiceImpl addCus end  resuly:"+num);
        if (num < 0) {
            responseMessage.setCode("500");
            responseMessage.setMessage("添加失败");
            logger.error("FinerCustomerServiceImpl addCus  error....");
            throw new RuntimeException("添加失败");
        }
        num = finerPhoneDao.addPhone(map);
        if (num < 0) {
            responseMessage.setCode("500");
            responseMessage.setMessage("添加失败");
            logger.error("FinerCustomerServiceImpl addCus  error....");
            throw new RuntimeException("添加失败");
        }
        responseMessage.setCode("200");
        responseMessage.setMessage("添加成功");
        return responseMessage;
    }

    @Override
    public ResponseMessage getCusById(Map map) {
        logger.info("FinerCustomerServiceImpl getCusById start....map:"+map);
        ResponseMessage responseMessage = new ResponseMessage();
        FinerCustomer finerCustomer = finerCustomerDao.getCusById(map);
        logger.debug("FinerCustomerServiceImpl getCusById end  resuly:"+finerCustomer);
        responseMessage.setCode("200");
        responseMessage.setData(finerCustomer);
        return responseMessage;
    }

    @Override
    public ResponseMessage updCus(Map map) {
        logger.info("FinerCustomerServiceImpl updCus start....map:"+map);
        ResponseMessage responseMessage = new ResponseMessage();
        int num = finerCustomerDao.updCus(map);
        logger.debug("FinerCustomerServiceImpl updCus end  resuly:"+num);
        if (num < 0) {
            responseMessage.setCode("500");
            responseMessage.setMessage("修改失败");
            logger.error("FinerCustomerServiceImpl updCus  error....");
            throw new RuntimeException("修改失败");
        }
        num = finerPhoneDao.updPhone(map);
        if (num < 0) {
            responseMessage.setCode("500");
            responseMessage.setMessage("修改失败");
            logger.error("FinerCustomerServiceImpl updCus  error....");
            throw new RuntimeException("修改失败");
        }
        responseMessage.setCode("200");
        responseMessage.setMessage("修改成功");
        return responseMessage;
    }

}
